package com.primary.controller;

import com.primary.common.AjaxResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
//import javax.servlet.http.HttpServletRequest; jdk8
import java.io.File;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author シン(Shen)
 * @time 2023/06/24 15:11 Saturday
 * @description TODO
 * @return
 */
@RestController
public class FileUploadController {

    /**
     * 上传
     */
    public final static String UPLOAD_PATH_PREFIX = "/upload/";

    @PostMapping(path = "/upload",headers = "Content-Type=multipart/form-data")
    /* @RequestParam 需要和前端传入参数名称一致 */
    public AjaxResult fileupload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) {

        if (uploadFile.isEmpty()) {
            //返回选择文件提示
            return AjaxResult.error("请选择上传文件");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        //构建文件上传所要保存的"文件夹路径"--这里是相对路径，保存到项目根路径的文件夹下
        String realPath = "src/main/resources/" + UPLOAD_PATH_PREFIX;
        String format = sdf.format(new Date());
        //存放上传文件的文件夹
        File file = new File(realPath + format);
        if (!file.isDirectory()) {
            //递归生成文件夹
            file.mkdirs();
        }
        //获取原始的名字  方法是得到原来的文件名在客户机的文件系统名称
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            //构建真实的文件路径
            File newFile = new File(file.getAbsolutePath() + File.separator + newName);
            //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
            uploadFile.transferTo(newFile);
            String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/upload/" + format + newName;
            return AjaxResult.success(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error("上传失败");
    }

}
